<!--
 * @Description: 操作下拉菜单组件
 * @Author: yue
 * @Date: 2022-09-01 10:16:17
 * @LastEditors: yue
 * @LastEditTime: 2022-09-03 16:11:18
-->

<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
    <el-col>
      <span>{{ obj.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>
          {{ obj.manager }}
        </el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'

export default {
  props: {
    obj: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async operateDepts(type) {
      if (type === 'add') {
        // 增
        this.$emit('addDept', this.obj)
      } else if (type === 'edit') {
        // 编辑
        this.$emit('editDept', this.obj)
      } else {
        // 删
        await this.$confirm('', '确认删除？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          center: true
        })
        await delDepartments(this.obj.id)
        this.$emit('delDept')
      }
    }
  }
}
</script>
